package com.goldgov.overview.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goldgov.overview.dao.OverviewDao;
import com.goldgov.overview.service.OverviewService;

@Service
public class OverviewServiceImpl implements OverviewService{

	@Autowired
	private OverviewDao overviewDao;
	
	@Override
	public Map<String, Object> listInfoItem(Date startDate, Date endDate) {
		List<String> projectNameList = overviewDao.listProjectName(startDate, endDate);
		List<String> userNameList = overviewDao.listUserName(startDate, endDate);
		//FIXME
		int exportCount = overviewDao.countByType("CODE_EXPORT",startDate, endDate);
		int addModuleCount = overviewDao.countByType("ADD_MODULE",startDate, endDate);
		int delModuleCount = overviewDao.countByType("REMOVE_MODULE",startDate, endDate);
		int importSqlModuleCount = overviewDao.countByType("IMPORT_MODULE_BY_SQL",startDate, endDate);
		int importJdbcModuleCount = overviewDao.countByType("IMPORT_MODULE_BY_JDBC",startDate, endDate);
		
		Map<String,Object> valueMap = new HashMap<>();
		valueMap.put("PROJECT_NAME", Arrays.toString(projectNameList.toArray(new String[projectNameList.size()])));
		valueMap.put("USER_NAME", Arrays.toString(userNameList.toArray(new String[userNameList.size()])));
		valueMap.put("CODE_EXPORT", exportCount);
		valueMap.put("ADD_MODULE", addModuleCount);
		valueMap.put("REMOVE_MODULE", delModuleCount);
		valueMap.put("IMPORT_MODULE_BY_SQL", importSqlModuleCount);
		valueMap.put("IMPORT_MODULE_BY_JDBC", importJdbcModuleCount);
		return valueMap;
	}

	@Override
	public Map<Long, Integer> listMonthActiveState() {
		Calendar calendar = Calendar.getInstance();
		int maximum = calendar.getMaximum(Calendar.DAY_OF_MONTH);
		int minimum = calendar.getMinimum(Calendar.DAY_OF_MONTH);
		calendar.set(Calendar.DAY_OF_MONTH, minimum);
		Date startDate = calendar.getTime();
		calendar.set(Calendar.DAY_OF_MONTH, maximum);
		Date endDate = calendar.getTime();
		
		List<Map<String, Object>> listLog = overviewDao.listLog(startDate, endDate);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		Map<Long, Integer> resultMap = new HashMap<>();
		for (int i = 0; i < listLog.size(); i++) {
			Map<String, Object> map = listLog.get(i);
			Date createDate = (Date)map.get("CREATE_DATE");
			String format = dateFormat.format(createDate);
			Long day;
			try {
				day = dateFormat.parse(format).getTime();
				int times = 1;
				if(resultMap.containsKey(day)) {
					Integer num = resultMap.get(day);
					times = num + 1;
				}
				resultMap.put(day, times);
			} catch (ParseException e) {
				throw new RuntimeException(format,e);
			}
		}
		return resultMap;
	}

}
